﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace ChicagoDealsN
{
    public class DLAdmin
    {
        private string strConnect = "Data Source=localhost\\sqlexpress;Initial Catalog=yvyas;Integrated Security=True";

        private string getConnection() { return strConnect; }

        public int DLToggleStatus(int id,int status)
        {
            int rowAffected=0;

            SqlConnection conPubs;
            SqlCommand cmdSelect = new SqlCommand();
            string Conn = getConnection();
            conPubs = new SqlConnection(Conn);
            try
            {
                conPubs.Open();

                cmdSelect = new SqlCommand("update Deals set Is_Active=@Status where Deal_Id=@DealId", conPubs);
                cmdSelect.Parameters.AddWithValue("@DealId", id);
                cmdSelect.Parameters.AddWithValue("@Status", status);

                rowAffected = cmdSelect.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conPubs.Close();
            }
          
            return rowAffected;
        }


        public DataSet DLGetAllUnApprovedDeals()
        {
            SqlConnection conPubs;
            SqlCommand cmdGetAll;
            string conn = getConnection();
            conPubs = new SqlConnection(conn);
            try
            {
                conPubs.Open();
                cmdGetAll = new SqlCommand("SELECT * FROM DEALS WHERE Is_Active=0 AND (DATEDIFF(day, End_Date, GETDATE()) <= 0)");
                DataSet set = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmdGetAll.CommandText, conPubs);
                da.Fill(set, "AllUnApprovedDealTable");
                conPubs.Close();
                return set;
            }
            catch (Exception e)
            {
                throw e;
            }

        }
    }
}